package com.yimi.appstore.view;


import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.yimi.appstore.R;

public class MyViewFlow {
	private Context context;
	private ViewFlow viewFlow;
	private ImageAdapter imageAdapter;
	private View parent;
	private int viewflowId, viewflowindicId;
	public MyViewFlow(Context context, View parent ,int viewflowId, int viewflowindicId){
		this.context=context;
		this.parent = parent;
		this.viewflowId=viewflowId;
		this.viewflowindicId=viewflowindicId;
		
	}
	public ViewFlow creatMyViewFlow(List<HashMap<String, Object>> listData,OnClickListener l , ViewPager viewPager){
		viewFlow = (ViewFlow)parent.findViewById(viewflowId);
		viewFlow.setPageView(viewPager);
		imageAdapter=new ImageAdapter(context, listData,l);
		viewFlow.setAdapter(imageAdapter);
		int size=listData.size();
		if(size>4){
			viewFlow.setmSideBuffer(size); // 实际图片张数
		}else{
			viewFlow.setmSideBuffer(4); // 实际图片张数
		}
	    CircleFlowIndicator indic = (CircleFlowIndicator) parent.findViewById(viewflowindicId);
        viewFlow.setFlowIndicator(indic);
        viewFlow.setSelection(3*1000);	//设置初始位置
        viewFlow.startAutoFlowTimer();  //启动自动播放 
		return viewFlow;
	}
	
	class ImageAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private List<HashMap<String, Object>> listData;
		private OnClickListener l ;
		public ImageAdapter(Context context, List<HashMap<String, Object>> listData , OnClickListener l) {
			mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.listData=listData;
			this.l = l;
		}
		public int getCount() {
			return Integer.MAX_VALUE;   //返回很大的值使得getView中的position不断增大来实现循环。
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.image_item, null);
			}
			HashMap<String, Object> item=listData.get(position%listData.size());
			ImageView iv= (ImageView)convertView.findViewById(R.id.imgView);
			if(item.containsKey("image")){
				iv.setImageBitmap((Bitmap)item.get("image"));
			} else {
				iv.setBackgroundResource(R.drawable.h3);
			}
			iv.setTag(position);
			iv.setOnClickListener(l);
			return convertView;
		}

	}
}
